package com.pktechan.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.CustBase;
import com.pktechan.api.entity.ProductBaseInfo;
import com.pktechan.api.entity.ProductChannelFee;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.ProductChannelFeeService;
import com.pktechan.api.service.ProductService;
import com.pktechan.api.service.RedisService;
import com.pktechan.api.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 产品相关功能
 * @author: qx.zhang
 * @create: 2021-03-05 11:27
 **/
@RestController
@RequestMapping("productController")
public class ProductController {

    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductChannelFeeService productChannelFeeService;
    @Autowired
    private RedisService redisService;
    @RequestMapping("getProductById")
    public void getProductById(@RequestParam Integer productId, HttpServletResponse response) {
        try {
            logger.warn("根据id=查询产品营销渠道费表");
            logger.info("根据id查询产品营销渠道费表---info");
            logger.debug("根据查询产品营销渠道费表---debug");
            ProductBaseInfo productBaseInfo = productService.getById(productId);
            HtmlUtil.writerSuccessJson(response, productBaseInfo);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 分页查询List
     *
     * @param productBaseInfo
     * @return
     */
    @RequestMapping("getProductList")
    public void getProductList(@RequestBody ProductBaseInfo productBaseInfo, HttpServletRequest request,HttpServletResponse response) {
        try {
            CustBase custBase = redisService.getCustBaseByUserLoginState(request.getParameter("userLoginState"));
            productBaseInfo.setCustVipType(custBase.getVipType());
            PageHelper.startPage(productBaseInfo.getPageNo(), productBaseInfo.getPageSize());
            List<ProductBaseInfo> list = productService.getList(productBaseInfo);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }



    /**
     * 根据id查询产品营销渠道费表
     *
     * @param channelFeeId
     * @param response
     */
    @RequestMapping(value = "getProductChannelFeeById", method = RequestMethod.POST)
    public void getProductChannelFeeById(@RequestParam Integer channelFeeId, HttpServletResponse response) {
        try {
            logger.warn("根据id=" + channelFeeId + "查询产品营销渠道费表");
            logger.info("根据id=" + channelFeeId + "查询产品营销渠道费表---info");
            logger.debug("根据id=" + channelFeeId + "查询产品营销渠道费表---debug");
            ProductChannelFee productChannelFee = productChannelFeeService.getById(channelFeeId);
            HtmlUtil.writerSuccessJson(response, productChannelFee);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 产品营销渠道费列表
     *
     * @param productChannelFee
     * @param response
     */
    @RequestMapping(value = "getProductChannelFeeList", method = RequestMethod.POST)
    public void getProductChannelFeeList(@RequestBody ProductChannelFee productChannelFee, HttpServletResponse response) {
        try {
            logger.info("产品营销渠道费列表");
            PageHelper.startPage(productChannelFee.getPageNo(), productChannelFee.getPageSize());
            List<ProductChannelFee> list = productChannelFeeService.getList(productChannelFee);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到产品营销渠道费表
     *
     * @param productChannelFee
     * @param response
     */
    @RequestMapping(value = "saveShopCart", method = RequestMethod.POST)
    public void saveShopCart(@RequestBody ProductChannelFee productChannelFee, HttpServletResponse response) {
        try {
            logger.info("添加到产品营销渠道费表");
            productChannelFeeService.save(productChannelFee);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改产品营销渠道费表
     *
     * @param productChannelFee
     * @param response
     */
    @RequestMapping(value = "updateShopCartById", method = RequestMethod.POST)
    public void updateShopCartById(@RequestBody ProductChannelFee productChannelFee, HttpServletResponse response) {
        try {
            logger.info("根据id=" + productChannelFee.getId() + "修改产品营销渠道费表");
            productChannelFeeService.update(productChannelFee);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 加盟产品列表
     *
     * @param productBaseInfo
     * @param response
     */
    @RequestMapping(value = "getProductJoinList", method = RequestMethod.POST)
    public void getProductJoinList(@RequestBody ProductBaseInfo productBaseInfo, HttpServletResponse response) {
        try {
            logger.info("加盟产品列表");
            List<ProductBaseInfo> list = productService.getProductJoinList(productBaseInfo);
            HtmlUtil.writerSuccessJson(response, list);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

}
